<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
<head>
    <title>花店销售分页列表</title>
    <script type="text/javascript" src="../js/jquery-3.5.1.min.js"></script>
    <script type="text/javascript">
        function checkDelete(id) {
            var ans = confirm("确认删除此记录吗？");
            if (ans) {
                location.href = "${pageContext.request.contextPath }/sys/delete?id=" + id;
                return true;
            }
            return false;
        }

        function jumpto(pageIndex,pageCount) {
            //验证用户的输入
            var regexp=/^[1-9]\d*$/;
            if(!regexp.test(pageIndex)){
                alert("请输入大于0的正整数！");
                return false;
            }else if((pageIndex-pageCount) > 0) {
                initList(pageCount);
            }else{
                initList(pageIndex);
            }
        }
    </script>
</head>
<body>
    <h2>花店销售列表</h2>
    <a href="../index.jsp">返回首页</a><br/>
    <table border="1" cellspacing="0" cellpadding="0" width="100%" id="tableTeachers">
        <tr>
            <td>销售单号</td>
            <td>名称</td>
            <td>是否收到</td>
            <td>销售日期</td>
            <td>工资</td>
            <td>头像</td>
            <td>籍贯</td>
            <td>操作</td>
        </tr>
    </table>
    <div id="divPage">
        <!-- 分页显示 -->
        <a href="listpage?pageIndex=1">首页</a>
        <a href="listpage?pageIndex=${page.pageIndex<page.pageCount?page.pageIndex+1:page.pageCount}">下一页</a>
        <a href="listpage?pageIndex=${page.pageIndex>1?page.pageIndex-1:1}">上一页</a>
        <a href="listpage?pageIndex=${page.pageCount}">末页</a>
        <!-- 显示页码 -->
        第${page.pageIndex}页 / 共${page.pageCount}页
        &nbsp;&nbsp;&nbsp;&nbsp;
        <!-- 页码跳转 -->
        跳转到：<input type="text" size="5" id="txtJumpto"  />
        <input type="button" value="GO" onclick="jumpto(document.forms[0],document.getElementById('txtJumpto').value,${page.pageCount})" />
    </div>
</body>

<script type="text/javascript">
    function initList(pageIndex){
        // $.json()方法
        $.getJSON("${pageContext.request.contextPath }/sys/list","pageIndex="+pageIndex,processList);
    }

    function processList(data) {
        // alert(data);
        $("#tableTeachers").empty();
        var strHead="<tr>" +
            "            <td>销售单号</td>" +
            "            <td>名称</td>" +
            "            <td>是否收到</td>" +
            "            <td>销售日期</td>" +
            "            <td>价格</td>" +
            "            <td>图片</td>" +
            "            <td>类别</td>" +
            "            <td>操作</td>" +
            "        </tr>";
        $("#tableTeachers").append(strHead);
        $(data.teachers).each(function () {
            var strInfo = "<tr><td>" + this.id + "</td>"
            + "<td>" + this.name + "</td>"
            + "<td>" + (this.isreceived?"是":"否") + "</td>"
            + "<td>" + this.sdate + "</td>"
            + "<td>" + this.price + "</td>"
            + "<td>" + "<img src=\"${pageContext.request.contextPath }/files/" + this.photo + "\" width=\"60px\" height=\"80px\" />"+ "</td>"
            + "<td>" + this.province.name + "</td>"
            + "<td>" + "<a href=\"${pageContext.request.contextPath }/sys/detail?id=" + this.id + "&edit=1\">修改</a>\n" +
                "                    <a href=\"javascript:checkDelete(" + this.id + ")\">删除</a>\n" +
                "                    <a href=\"${pageContext.request.contextPath }/sys/detail?id=" + this.id + "\">查看</a>" + "</td>"
            + "</tr>";
            $("#tableTeachers").append(strInfo);
            // alert(strInfo);
        });

        // 渲染分页导航
        var divPage = $("#divPage").empty(); //显示分页的父容器
        divPage.append("<a href=\"javascript:initList(1)\">首页</a>&nbsp;");
        divPage.append("<a href=\"javascript:initList(" + (data.page.pageIndex<data.page.pageCount?(data.page.pageIndex + 1):data.page.pageCount) + ")\">下一页</a>&nbsp;");
        divPage.append("<a href=\"javascript:initList(" + (data.page.pageIndex>1?(data.page.pageIndex - 1):1) + ")\">上一页</a>&nbsp;");
        divPage.append("<a href=\"javascript:initList(" + (data.page.pageCount) + ")\">末页</a>");

        divPage.append("&nbsp;&nbsp;第[" + data.page.pageIndex + "页 / 共" + data.page.pageCount + "页");

        // 页码跳转
        divPage.append("&nbsp;&nbsp;&nbsp;&nbsp;&跳转到：<input type=\"text\" size=\"5\" id=\"txtJumpto\"  />");
        divPage.append('<input type="button" value="GO" onclick="jumpto($(\'#txtJumpto\').val(),' + data.page.pageCount + ')" />');
    }

    $(document).ready(function () {
        initList(1);
    });
</script>
</html>
